import json

from utils.common import extract_code_block


def gen_prompt(cli_text: str) -> str:
    
    # 编写prompt，大意为；根据如上的json，检查如下的cli指令中：
    with open("examples/vtysh_merged.json", 'r', encoding='utf-8') as f:
        vtysh_config = json.load(f)
        prompt = "```json\n"
        prompt += json.dumps(vtysh_config, ensure_ascii=False, indent=2)
        prompt += "\n```\n\n"
        prompt += (
            "## Please check whether the following CLI commands comply with the node, view, and available commands defined in the JSON above:\n\n"
            "1. Are the CLI commands under the nodes defined in the JSON?\n"
            "2. Are the CLI commands under the views defined in the JSON?\n"
            "3. Are the CLI commands in the list of available commands defined in the JSON?\n"
        )
        prompt += "Please check the following CLI commands one by one:\n\n"
        prompt += "```text\n"
        prompt += cli_text.strip()
        prompt += "\n```\n\n"
        prompt += "Re-output the CLI commands, ensuring they comply with the nodes, views, and available commands defined in the JSON above.\n"
        prompt += "## Output Format\n\n"
        prompt += (
            "Please provide the CLI commands for the test steps described above in the following format:\n\n"
            "```\n"
            "# Step 1: Description\n"
            "# Necessary explanation\n"
            "CLI Command 1\n"
            "CLI Command 2\n\n"
            "# Step 2: Description\n"
            "# Necessary explanation\n"
            "CLI Command 3\n"
            "(and so on...)\n"
            "```"
        )
        prompt += "\n\nAdditional Requirements:\n"
        prompt += "1. Ensure the output contains exactly one code block enclosed within triple backticks (```). All content outside the code block will be ignored.\n"
        prompt += "2. Each CLI command must include the prompt indicating the current view, ensuring that transitions between views are explicitly stated.\n"
        prompt += "3. Assume that multiple commands within the same code block will be executed sequentially. Consider dependencies between commands, including prerequisite or post-requisite steps, and transitions between views.\n"
        prompt += "4. If the generated commands include any uncertain parameters or variables, use the <var-name> format to explicitly represent configurable parameters.\n"
        return prompt
    
def main(llm, cli_text: str, verbose: bool = False) -> str:
    """
    主函数，处理命令行参数并执行网络配置
    """
    # 生成提示信息
    prompt = gen_prompt(cli_text)

    if verbose:
        print("Generated Prompt:")
        print(prompt)

    reply = llm.send_request(prompt)
    
    if reply is None:
        raise ValueError("No reply received from the LLM.")
    if verbose:
        print("LLM Reply:")
        print(reply)
        print("Reasoning:")
    
    cli_code = extract_code_block(reply)
    if verbose:
        print("Generated CLI Commands:")
        print(cli_code)

    return cli_code
    



